layui.define(['table', 'form'], function(exports){
	var $ = layui.$,table = layui.table,form = layui.form;

	var tradeNo = $('input[name="tradeNo"]').val();

	table.render({
		elem: '#LAY-logs-table'
		,url: layui.setter.webRoot + '/market/order/logs/list.data'
		,where:{tradeNo:tradeNo}
		,method: 'post'
		,parseData: function (res) {
			return {
				"code": res.code,
				"msg": res.msg,
				"count": res.body.total,
				"data": res.body.data
			};
		}
		,response: {statusCode: 200}
		,cols: [[
			{field: 'createDate', title: '操作时间'}
			,{field: 'operateContent', title: '操作内容'}
			,{field: 'programType', title: '操作程序'}
			,{field: 'operatorTypeName', title: '操作者类型'}
			,{field: 'operator', title: '操作者信息'}
			,{field: 'operateStateName', title: '操作状态'}
			,{field: 'operateError', title: '错误信息'}
		]]
		,page: false
		,height: 'full'
		,text: '对不起，加载出现异常！'
	});

	var active = {
		btnStateSearch:function (){
			layer.confirm('确认要查询订单的支付状态吗？', function(index){
				$.ajax({
					type: "post",
					url: layui.setter.webRoot + '/market/order/goods/state/search.do',
					data:{tradeNo:tradeNo},
					dataType:"json",
					success:function(res){
						if(res.code == 200){
							layer.msg('操作成功',{icon:1},function (){
								location.reload();
							});
						}else{
							layer.msg(res.msg,{icon: 5});
						}
					}
				});
				layer.close(index);
			});
		}
		,btnOrderConfirm:function (){
			layer.confirm('确认后用户将无法再修改收货地址，确定有库存并且物流可达吗？', function(index){
				$.ajax({
					type: "post",
					url: layui.setter.webRoot + '/market/order/goods/confirm.do',
					data:{tradeNo:tradeNo},
					dataType:"json",
					success:function(res){
						if(res.code == 200){
							layer.msg('操作成功',{icon:1},function (){
								location.reload();
							});
						}else{
							layer.msg(res.msg,{icon: 5});
						}
					}
				});
				layer.close(index);
			});
		}
		,btnDeliverGoods: function(){
			layer.open({
				type: 2
				,title: '发货'
				,content: layui.setter.webRoot + '/market/order/goods/deliver.html?tradeNo='+tradeNo
				,maxmin: true
				,area: ['500px', '90%']
				,btn: ['确定', '取消']
				,yes: function(index, layero){
					var iframeWindow = window['layui-layer-iframe'+ index]
						,submitID = 'LAY-form-submit'
						,submit = layero.find('iframe').contents().find('#'+ submitID);
					iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						var field = data.field; //获取提交的字段
						$.ajax({
							type: "post",
							url: layui.setter.webRoot + '/market/order/goods/deliver.do',
							data:field,
							dataType:"json",
							success:function(res){
								if(res.code == 200){
									layer.close(index); //关闭弹层
									layer.msg('操作成功',{icon:1},function (){
										location.reload();
									});
								}else{
									layer.msg(res.msg,{icon: 2});
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
		}
		,btnReceiveGoods: function(){
			layer.open({
				type: 2
				,title: '确认收货'
				,content: layui.setter.webRoot + '/market/order/goods/receive.html?tradeNo='+tradeNo
				,maxmin: true
				,area: ['500px', '80%']
				,btn: ['确定', '取消']
				,yes: function(index, layero){
					var iframeWindow = window['layui-layer-iframe'+ index]
						,submitID = 'LAY-form-submit'
						,submit = layero.find('iframe').contents().find('#'+ submitID);
					iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						var field = data.field; //获取提交的字段
						$.ajax({
							type: "post",
							url: layui.setter.webRoot + '/market/order/goods/receive.do',
							data:field,
							dataType:"json",
							success:function(res){
								if(res.code == 200){
									layer.close(index); //关闭弹层
									layer.msg('操作成功',{icon:1},function (){
										location.reload();
									});
								}else{
									layer.msg(res.msg,{icon: 2});
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
		}
		,btnEvaluateGoods: function(){
			layer.open({
				type: 2
				,title: '默认评价'
				,content: layui.setter.webRoot + '/market/order/goods/evaluate.html?tradeNo='+tradeNo
				,maxmin: true
				,area: ['500px', '70%']
				,btn: ['确定', '取消']
				,yes: function(index, layero){
					var iframeWindow = window['layui-layer-iframe'+ index]
						,submitID = 'LAY-form-submit'
						,submit = layero.find('iframe').contents().find('#'+ submitID);
					iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
						var field = data.field; //获取提交的字段
						$.ajax({
							type: "post",
							url: layui.setter.webRoot + '/market/order/goods/evaluate.do',
							data:field,
							dataType:"json",
							success:function(res){
								if(res.code == 200){
									layer.close(index); //关闭弹层
									layer.msg('操作成功',{icon:1},function (){
										location.reload();
									});
								}else{
									layer.msg(res.msg,{icon: 2});
								}
							}
						});
					});
					submit.trigger('click');
				}
			});
		}
	}

	$('.layui-btn.layui-btn-fun').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	exports('market_business_order_info', {})
});